<?php

class News_Application_CacheManager
{
    function News_Application_CacheManager($controller)
    {
        $this->Controller = $controller;
        $this->ClearSubscribed();
		$this->Path = './cache.log';
    }
	
	function Log ($msg)
	{
		$line = date('d-m-y h:i:s') . ' ' . $msg . "\n";
		echo $line;
		$fp = fopen ($this->Path, 'a');
		fwrite ($fp, $line);
		fclose ($fp);
    }
	
	function Rmdir ($path)
	{ 
	    passthru ("rm -rf {$path}");
		$this->Log (is_dir($path) ? "Unable to delete {$path}" : "Deleted directory {$path}");
	}
	
	function Rm ($path)
	{ 
		unlink ($path);  
		$this->Log (file_exists($path) ? "Unable to delete {$path}" : "Deleted file {$path}");
	}
    
    function Invoke ($command)
    {
    	$this->Path = $command['path'];
		if (file_exists($this->Path))
		{
			$tmp = $this->Controller->Read ($this->Path); echo $tmp;
			parse_str($tmp);
			foreach ($evict as $x) $this->Rmdir($x); 
		}
		unlink ($this->Path);
	}
    
    function ClearSubscribed ()
    {
        $my = $this->Controller->GetSubscribedGroups ();
        $ex = scandir ('data'); 
        $ret = array ();
        foreach ($ex as $file)
        {
            if (strpos($file, '.xml')!==false && strpos($file, 'alt.')!==false)
            {
                $name = str_replace ('.xml', '', $file);
                $comma = strpos($name, ',');
                if ($comma !== false)
                {
                    $name = substr($name, 0, $comma); 
                }
                
                if (array_search ($name, $my) === false)
                    $ret[] = "evict[]=data/{$file}";
            }
        }
        
        $ex = scandir ('data/temp');
        foreach ($ex as $dir)
        {
            $name = "data/temp/{$dir}";
            if (is_dir($name) && strpos($dir, 'alt.')!==false)
            { 
                if (array_search ($dir, $my) === false)
                    $ret[] = "evict[]={$name}";
            }
        }
		 
		$this->Controller->Save ("notify/maintain.cache", implode ('&', $ret));
        
    }
}



















